Skip to content

Conversation

@uyeon0
Copy link
Collaborator

@uyeon0 uyeon0 commented Oct 8, 2025

User description

오늘도 멋져요 👍✨


PR Type

Enhancement


Description

  • 프로그래머스 예상 대진표 문제 해결 추가

  • 프로그래머스 문제 노트 노션 링크 추가


@uyeon0 uyeon0 added the programmers Programmers 문제 풀이 label Oct 8, 2025
@github-actions
Copy link

github-actions bot commented Oct 8, 2025

PR Reviewer Guide 🔍

🧪 PR contains tests
⚡ Recommended focus areas for review

성능 개선

이진 탐색 대신 비트 연산을 사용하여 O(log n)에서 O(1)로 개선 가능합니다. a와 b의 위치 차이를 비트 연산으로 계산하면 라운드 수를 즉시 구할 수 있습니다.

while (round >= 1) {
  // a와 b의 현재 위치 확인
  const mid = (left + right) / 2;
  const aIsLeft = a <= mid;
  const bIsLeft = b <= mid;
  // 둘이 다른 절반에 있다면 현재 라운드에서 만난다.
  if (aIsLeft !== bIsLeft) return round;
  // 둘이 같은 절반에 있다면 범위 업데이트
  if (aIsLeft) [left, right] = [left, Math.floor(mid)];
  else [left, right] = [Math.floor(mid), right];
  // 라운드 줄이기
  round--;
}
엣지 케이스

연속된 번호(예: a=3, b=4)나 멀리 떨어진 번호(예: a=1, b=n)에 대한 처리가 필요할 수 있습니다. 현재 구현은 이러한 극단적인 경우에서 잘못된 결과를 반환할 수 있습니다.

function solution(n, a, b) {
  let round = Math.log2(n); // 최종 라운드로 초기화, 줄여나갈 예정
  let [left, right] = [1, n];

  // 이진 탐색
  while (round >= 1) {
    // a와 b의 현재 위치 확인
    const mid = (left + right) / 2;
    const aIsLeft = a <= mid;
    const bIsLeft = b <= mid;
    // 둘이 다른 절반에 있다면 현재 라운드에서 만난다.
    if (aIsLeft !== bIsLeft) return round;
    // 둘이 같은 절반에 있다면 범위 업데이트
    if (aIsLeft) [left, right] = [left, Math.floor(mid)];
    else [left, right] = [Math.floor(mid), right];
    // 라운드 줄이기
    round--;
  }

  return round;
}

@github-actions
Copy link

github-actions bot commented Oct 8, 2025

PR Code Suggestions ✨

@yoouyeon yoouyeon added the ready-to-merge pr을 머지해주세요 label Oct 21, 2025
@uyeon0 uyeon0 merged commit 25bab1d into main Oct 21, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

programmers Programmers 문제 풀이 ready-to-merge pr을 머지해주세요

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants